package com.dd.machine.main.user;

import android.content.Intent;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.text.Editable;
import android.text.TextWatcher;
import android.util.Log;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.WindowManager;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.EditText;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.PopupWindow;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.cn.dd.machine.R;
import com.dd.machine.base.activity.BaseActivity;
import com.dd.machine.base.app.MyApplication;
import com.dd.machine.base.utils.ImageLoaderUtils;
import com.dd.machine.base.utils.ToastUtil;
import com.dd.machine.greendao.gen.AppGoodsKindDao;
import com.dd.machine.greendao.gen.EqueipmentDao;
import com.dd.machine.greendao.gen.OrderFormDao;
import com.dd.machine.main.HardwareUtils.LockUtils;
import com.dd.machine.main.HardwareUtils.PrinterUtils;
import com.dd.machine.main.HardwareUtils.WeightUtils;
import com.dd.machine.main.commons.VoiceUtils;
import com.dd.machine.main.desktop.DeskTopActivity;
import com.dd.machine.main.user.adapter.GoodsKindDetailsAdapter;
import com.dd.machine.main.user.model.AppGoodsKind;
import com.dd.machine.main.user.model.Equeipment;
import com.dd.machine.main.user.model.OrderForm;
import com.ensentec.Lib.EnsenLib;
import com.ensentec.Lib.EnsenLibException;
import com.ensentec.Lib.OverWeightException;
import com.google.gson.Gson;
import com.zhy.http.okhttp.OkHttpUtils;
import com.zhy.http.okhttp.callback.StringCallback;

import org.greenrobot.greendao.query.Query;
import org.json.JSONException;
import org.json.JSONObject;

import java.math.BigDecimal;
import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;
import okhttp3.Call;
import okhttp3.MediaType;

import static com.dd.machine.base.net.URLManager.BUILDOEDER;
import static com.dd.machine.base.net.URLManager.USERLOGOUT;
import static com.dd.machine.main.commons.OrderCodeUtil.createOrderCode;

public class KindDetailsActivity extends BaseActivity {

    @Bind(R.id.activity_detail)
    LinearLayout activity_detail;
    @Bind(R.id.back)
    RelativeLayout back;
    @Bind(R.id.bar)
    RelativeLayout bar;
    @Bind(R.id.tv1)
    TextView tv1;
    @Bind(R.id.type_nameTv)
    TextView typeNameTv;
    @Bind(R.id.type_iv)
    ImageView typeIv;
    @Bind(R.id.iv_rel)
    RelativeLayout ivRel;
    @Bind(R.id.kind_ad_tv)
    TextView kindAdTv;
    @Bind(R.id.kind_ad_rel)
    RelativeLayout kindAdRel;
    @Bind(R.id.samall_kind_gv)
    GridView samallKindGv;

    Bundle bundle;
    @Bind(R.id.use_tv)
    TextView useTv;
    @Bind(R.id.goods_prices_tv)
    TextView goodsPricesTv;
    @Bind(R.id.unit_tv)
    TextView unitTv;
    @Bind(R.id.weightOrNumber)
    TextView weightOrNumber;
    @Bind(R.id.wait_weight_tv)
    TextView waitWeightTv;
    @Bind(R.id.weight_tv)
    TextView weightTv;
    @Bind(R.id.after_weight_lin)
    LinearLayout afterWeightLin;
    @Bind(R.id.number_sub)
    RelativeLayout numberSub;
    @Bind(R.id.number_et)
    EditText numberEt;
    @Bind(R.id.number_add)
    RelativeLayout numberAdd;
    @Bind(R.id.number_lin)
    LinearLayout numberLin;
    @Bind(R.id.money_tv)
    TextView moneyTv;
    @Bind(R.id.huibao_lin)
    LinearLayout huibaoLin;
    @Bind(R.id.data)
    LinearLayout data;
    @Bind(R.id.btn_open)
    Button btnOpen;
    @Bind(R.id.btn_new_selected)
    Button btnNewSelected;
    @Bind(R.id.btn_sure)
    Button btnSure;
    @Bind(R.id.after_weight_rel)
    LinearLayout afterWeightRel;
    @Bind(R.id.mesure_weight)
    TextView mesureWeight;
    @Bind(R.id.analogEt)
    EditText analogEt;

    private List<AppGoodsKind> appFixPriceList;
    private GoodsKindDetailsAdapter adapter;
    String typename;
    String id;
    String measurement;
    int selectedId;
    AppGoodsKindDao dao;

    boolean isKg = true;

    private static final int MSG_UPDATE_MONEY = 1;//更新回报
    private static final int MSG_UPDATE_SHOW = 0;//更新显示
    double weightOrNums;
    private double price;

    private String userId;


    private int equipmentId;//机器id
    private String equipmentNo;//机器编码
    private OrderForm orderForm;

    private String goodsInfo;//订单信息

    private int amount = 0;

    private boolean isClose = true;//是否关闭好箱门
    private boolean retIsClser = true;//循环监测用的
    private boolean isGv = true;//是否可以点击小分类
    private boolean isNumber = true;//是否可以点击 数量 加减


    private PopupWindow popupWindow_success;
    private PopupWindow popupWindow_failed;

    private static com.baidu.tts.client.SpeechSynthesizer mSpeechSynthesizer;
    private String orderCode;
    private LockUtils lockUtils;
    private WeightUtils weightUtils;
    private PrinterUtils printerUtils;

    private static final String TAG = "MainActivity";
    private static final String Rod1 = "LEFT";
    private static final String Rod2 = "RIGHT";


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_kind_details);
        ButterKnife.bind(this);
        getEqumentMessage();
        init();
        initData();
        initPopupWindow_Success();
        initPopupWindow_Failed();
        initUtils();


    }

    private void initUtils() {
        VoiceUtils voiceUtils = MyApplication.getInstances().getVoiceUtils();
        mSpeechSynthesizer = voiceUtils.getSyntheszer();
//        weightUtils = new WeightUtils(this);
//        lockUtils = new LockUtils(this);
//        printerUtils = new PrinterUtils(this);
    }

    @Override
    protected void onResume() {
        super.onResume();
        mSpeechSynthesizer.speak("请确认分类并确认已正确打包");
    }

    private void getEqumentMessage() {
        EqueipmentDao equeipmentDao = MyApplication.getInstances().getDaoSession().getEqueipmentDao();
        List<Equeipment> equeipmentList = equeipmentDao.loadAll();
        equipmentId = equeipmentList.get(0).getEId();
        equipmentNo = equeipmentList.get(0).getEquipmentNo();
    }

    private void initPopupWindow_Success() {
        View view = LayoutInflater.from(this).inflate(R.layout.popup_put_success, null);
        Button logout_btn = (Button) view.findViewById(R.id.logout_btn);
        Button goon_btn = (Button) view.findViewById(R.id.goon_btn);


        popupWindow_success = new PopupWindow(view, RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT, false);
        popupWindow_success.setFocusable(true);
        popupWindow_success.setOutsideTouchable(true);
        ColorDrawable dw = new ColorDrawable(0x00000000);
        popupWindow_success.setBackgroundDrawable(dw);

        popupWindow_success.setOnDismissListener(new PopupWindow.OnDismissListener() {
            @Override
            public void onDismiss() {
                backgroundAlpha(1f);
            }
        });

        logout_btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                popupWindow_success.dismiss();
                //应该是执行电机转动，将物品推下去

                logout();
            }
        });

        goon_btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //应该是执行电机转动，将物品推下去
                popupWindow_success.dismiss();
                finish();
            }
        });

    }

    private void initPopupWindow_Failed() {
        View view = LayoutInflater.from(this).inflate(R.layout.popup_put_failed, null);
        Button logout_btn_fail = (Button) view.findViewById(R.id.logout_btn_fail);
        Button new_btn = (Button) view.findViewById(R.id.new_btn);


        popupWindow_failed = new PopupWindow(view, RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT, false);
        popupWindow_failed.setFocusable(true);
        popupWindow_failed.setOutsideTouchable(true);
        ColorDrawable dw = new ColorDrawable(0x00000000);
        popupWindow_failed.setBackgroundDrawable(dw);

        popupWindow_failed.setOnDismissListener(new PopupWindow.OnDismissListener() {
            @Override
            public void onDismiss() {
                backgroundAlpha(1f);
            }
        });

        logout_btn_fail.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //投放失败，退出登录，打开箱门，将物品取出
                EnsenLib.getInstance().openLock(1);
                logout();

            }
        });
        new_btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //此时应该开门，提示用户取出物品重新投放


                //开门
                EnsenLib.getInstance().openLock(1);


                //语音播报
                mSpeechSynthesizer.speak("请取出物品，并关好箱门，重新投放");

                //检查是否关好箱门，关好之后，页面退回到分类页面，进行重新选择

                //使用thread延迟2秒，模拟用户取出物品进行关箱门
                ToastUtil.showShortToast(KindDetailsActivity.this,"在此等待2秒，模拟取出物品关好箱门的操作。在此缺少监听箱门状态事件，日后需要添加");
                new Thread(new Runnable() {
                    @Override
                    public void run() {
                        try {
                            Thread.sleep(2000);
                            popupWindow_success.dismiss();
                            finish();
                            overridePendingTransition(R.anim.back_anim, R.anim.backback_anim);
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }

                    }
                });
            }
        });
    }

    private void init() {
        bundle = getIntent().getExtras();
        typename = bundle.getString("name");
        measurement = bundle.getString("measurement");
        if ("千克".equals(measurement) || "公斤".equals(measurement)) {
            isKg = true;
        } else {
            isKg = false;
        }
        id = bundle.getString("id");
        Log.e("===id", id);
        userId = bundle.getString("userId");
        typeNameTv.setText(typename);
        numberEt.setText(amount + "");
//        weightTv.setInputType(InputType.TYPE_NULL);
//        analogEt.setInputType(InputType.TYPE_NULL);
        //隐藏在某处的输入框，接收称



        numberEt.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {
                try {
                    weightOrNums = Double.valueOf(s.toString());
                    Message msg = Message.obtain();
                    msg.what = 1;
                    handler.sendMessage(msg);
                } catch (Exception e) {

                }
            }
        });
    }

    private void initData() {
        appFixPriceList = new ArrayList<>();
        dao = MyApplication.getInstances().getDaoSession().getAppGoodsKindDao();
        appFixPriceList = dao.queryBuilder().where(AppGoodsKindDao.Properties.GoodsId.eq(id)).list();
        Log.e("==size", appFixPriceList.size() + "");
        adapter = new GoodsKindDetailsAdapter(this, appFixPriceList);
        samallKindGv.setAdapter(adapter);
        if (appFixPriceList.size() > 0) {
            selectedId = appFixPriceList.get(0).getGoodsId();
            Message msg = Message.obtain();
            msg.what = MSG_UPDATE_SHOW;
            handler.sendMessage(msg);
        }

        adapter.notifyDataSetChanged();

        //当用户选择好小分类，并把物品 投放后，其他小分类将不能再点击选择
        if (isGv) {
            samallKindGv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                @Override
                public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                    selectedId = appFixPriceList.get(position).getGoodsId();
                    Message msg = Message.obtain();
                    msg.what = MSG_UPDATE_SHOW;
                    handler.sendMessage(msg);
                }
            });
        }

    }

    public Handler handler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            switch (msg.what) {
                case MSG_UPDATE_SHOW:
                    show();
                    break;
                case MSG_UPDATE_MONEY:
                    moneyTv.setText(formatDouble(price * weightOrNums));
                    break;
            }
        }
    };

    private void show() {
        for (int i = 0; i < appFixPriceList.size(); i++) {
            if (appFixPriceList.get(i).getGoodsId() == selectedId) {
                adapter.setSelectItemPosition(i);
                adapter.notifyDataSetChanged();
                ImageLoaderUtils.display(appFixPriceList.get(i).getImg(), typeIv);
                price = appFixPriceList.get(i).getUnitPrice();
                goodsPricesTv.setText(appFixPriceList.get(i).getUnitPrice() + "");
                unitTv.setText("元/" + measurement);
                btnOpen.setVisibility(View.VISIBLE);
                if (isKg) {
                    mesureWeight.setText("    " + measurement);
                    weightOrNumber.setText("重量");
                    waitWeightTv.setVisibility(View.VISIBLE);
                    afterWeightLin.setVisibility(View.GONE);
                    numberLin.setVisibility(View.GONE);
                    huibaoLin.setVisibility(View.GONE);
                    afterWeightRel.setVisibility(View.GONE);

                } else {
                    weightOrNumber.setText("数量");
                    waitWeightTv.setVisibility(View.GONE);
                    afterWeightLin.setVisibility(View.GONE);
                    numberLin.setVisibility(View.VISIBLE);
                    huibaoLin.setVisibility(View.GONE);
                    afterWeightRel.setVisibility(View.GONE);
                }
            }

        }
    }

    @OnClick({R.id.back, R.id.number_sub, R.id.number_add, R.id.btn_open, R.id.btn_new_selected, R.id.btn_sure})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.back:
                finish();
                overridePendingTransition(R.anim.back_anim, R.anim.backback_anim);
                break;
            case R.id.number_sub:
                if (isNumber) {
                    if (amount > 0) {
                        amount--;
                        numberEt.setText(amount + "");
                    }
                }

                break;
            case R.id.number_add:
                if (isNumber) {
                    amount++;
                    numberEt.setText(amount + "");
                }

                break;
            case R.id.btn_open:
                //点击确认，打印小票，打开箱门。语音提示:贴码，投放，关门
                isGv = false;
                //生成订单号
                orderCode = createOrderCode(equipmentNo);
                //执行打印条码操作
                ToastUtil.showShortToast(this,"打印");
                try {
                    EnsenLib.getInstance().print(typename, orderCode);
                } catch (Exception e) {
                    Log.e(TAG, "onClick: Printer print Err:" +
                            e.toString());
                }


//                printerUtils.setPrinter(typename,orderCode);
                //执行打开箱门操作 默认打开1号门
//                lockUtils.setOpenLock(1);
                ToastUtil.showShortToast(this,"打开1号门");
                EnsenLib.getInstance().openLock(1);

                if (isKg) {
                    btnOpen.setVisibility(View.GONE);
                    afterWeightLin.setVisibility(View.VISIBLE);
                    weightTv.setFocusable(true);
                    huibaoLin.setVisibility(View.VISIBLE);
                    waitWeightTv.setVisibility(View.GONE);
                    afterWeightRel.setVisibility(View.VISIBLE);
                    mSpeechSynthesizer.speak("请在打包好的物品上贴条码，投放后关好箱门");

                    //等待5秒，获取称重质量，模拟投放、关门的操作 日后需要改方法
                    new Thread(new Runnable() {
                        @Override
                        public void run() {
                            try {
                                Thread.sleep(5 * 1000);
//                                weightUtils.getGoodsWeight();
                                try {
                                    int weight = EnsenLib.getInstance().getWeight();
                                    weightTv.setText((weight / 1000) + "");
                                    weightOrNums = weight;
                                    Message msg = Message.obtain();
                                    msg.what = 1;
                                    handler.sendMessage(msg);
                                } catch (EnsenLibException e) {//称重有异常，弹出失败，
                                    Log.e(TAG, "onClick: get Weight Err: " + e.getMessage());
                                    popupWindow_failed.showAtLocation(activity_detail, Gravity.CENTER, 0, 0);
                                } catch (OverWeightException e) {
                                    Log.e(TAG, "onClick: get Weight Err: " + e.getMessage());
                                    popupWindow_failed.showAtLocation(activity_detail, Gravity.CENTER, 0, 0);
                                }

                            } catch (InterruptedException e) {
                                e.printStackTrace();
                            }
                        }
                    });
                } else {
                    Log.e("===数量", numberEt.getText().toString());
                    if (numberEt.getText().toString().equals("0") || "".equals(numberEt.getText().toString())) {
                        mSpeechSynthesizer.speak("请选择投放数量");
                    } else {

                        isNumber = false;

                        huibaoLin.setVisibility(View.VISIBLE);
                        afterWeightRel.setVisibility(View.VISIBLE);
                        btnOpen.setVisibility(View.GONE);
                        afterWeightLin.setVisibility(View.GONE);
                        mSpeechSynthesizer.speak("请在打包好的物品上贴条码，投放后关好箱门");
                    }
                }
                break;
            case R.id.btn_new_selected:
                //放弃投放，重新选择，打开箱门，提示用户取回物品，并关闭箱门
                mSpeechSynthesizer.speak("请将物品取出并关好箱门");
                EnsenLib.getInstance().openLock(1);
                ToastUtil.showShortToast(this, "开门，取物，关门,延迟3秒后退出改该界面");//需要弹窗提示，待设计
                //检查是否关好箱门，关好之后执行下一步操作
                new Thread(new Runnable() {//模拟时间
                    @Override
                    public void run() {
                        try {
                            Thread.sleep(3 * 1000);
                            finish();
                            overridePendingTransition(R.anim.back_anim, R.anim.backback_anim);
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }
                    }
                });

                break;
            case R.id.btn_sure:

                //点击确认投放，生成订单
                buildOrderForm();
                //上传订单
                sendOrderForm(orderForm);

                break;
        }
    }


    private void buildOrderForm() {
        orderForm = new OrderForm();
        orderForm.setEquipmentId(equipmentId);
        orderForm.setOrderCode(orderCode);
        orderForm.setStatus(0);
        orderForm.setUnitPrice(price);
        orderForm.setOrderType("sell_waste");
        if (isKg) {
            orderForm.setGoodsWeight(weightOrNums);
            orderForm.setGoodsCount(0);
        } else {
            orderForm.setGoodsWeight((double) 0);
            orderForm.setGoodsCount((int) weightOrNums);
        }
        orderForm.setGoodsName(typename);
        double f = price * weightOrNums;
        BigDecimal b = new BigDecimal(f);
        double f1 = b.setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue();
        orderForm.setTotalPrice(f1);
        orderForm.setUserId(userId);
        orderForm.setCreateTime(System.currentTimeMillis());//添加创建时间，用来定时处理用
        orderForm.setUpdateTime("0");
        orderForm.setImgUrl("");
        orderForm.setOtherCostType("0");
        orderForm.setRemark("");
        orderForm.setClassifyRightFlag(1);
        orderForm.setPackRightFlag(1);

    }

    private void sendOrderForm(final OrderForm orderForm) {
        Gson gson = new Gson();
        goodsInfo = "[" + gson.toJson(orderForm) + "]";
        Log.e("====goodsInfo", goodsInfo);

        //订单上传不管成功与否，只要与打包物品无关，就提示上传成功，大不了日后再统一上传一次
        OkHttpUtils.postString()
                .url(BUILDOEDER)
                .content(goodsInfo)
                .mediaType(MediaType.parse("application/json;charset=UTF-8"))
                .build()
                .execute(new StringCallback() {
                    @Override
                    public void onError(Call call, Exception e, int id) {
                        Log.e("===sendError", e.getMessage());
                        //请求服务器失败，也需要上传本地数据库
                        cacheOrderInfo(orderForm);
                        backgroundAlpha(0.4f);
                        popupWindow_success.showAtLocation(activity_detail, Gravity.CENTER, 0, 0);
                        mSpeechSynthesizer.speak("完成投放，请耐心等待收益到账");

                    }

                    @Override
                    public void onResponse(String response, int id) {
                        Log.e("===sendResponse", response);
                        try {
                            JSONObject object = new JSONObject(response);
                            String result = object.optString("result");
                            if ("1".equals(result)) {//上传成功
                                mSpeechSynthesizer.speak("完成投放，请耐心等待收益到账");
                                backgroundAlpha(0.4f);
                                popupWindow_success.showAtLocation(activity_detail, Gravity.CENTER, 0, 0);
                            } else {//上传失败
                                //保存到本地
                                cacheOrderInfo(orderForm);
                                backgroundAlpha(0.4f);
                                popupWindow_success.showAtLocation(activity_detail, Gravity.CENTER, 0, 0);
                                mSpeechSynthesizer.speak("完成投放，请耐心等待收益到账");
                            }
                        } catch (JSONException e) {
                            e.printStackTrace();
                        }
                    }
                });

    }

    private void cacheOrderInfo(OrderForm orderForm) {
        OrderForm old = getInfoFromCache(orderForm.getOrderCode());
        if (old != null) {
            orderForm.setId(old.getId());
        }
        OrderFormDao dao = MyApplication.getInstances().getDaoSession().getOrderFormDao();
        dao.save(orderForm);
    }

    private OrderForm getInfoFromCache(String orderCode) {
        OrderFormDao dao = MyApplication.getInstances().getDaoSession().getOrderFormDao();
        Query<OrderForm> query = dao.queryBuilder().where(OrderFormDao.Properties.OrderCode.eq(orderCode))
                .orderDesc(OrderFormDao.Properties.OrderCode).build();
        return query.unique();

    }

    public static String formatDouble(double d) {
        DecimalFormat df = new DecimalFormat("######0.00");
        return df.format(d);
    }

    //设置背景色
    public void backgroundAlpha(float bgAlpha) {
        WindowManager.LayoutParams lp = getWindow().getAttributes();
        lp.alpha = bgAlpha; //0.0-1.0
        getWindow().setAttributes(lp);
    }

    private void logout() {
        OkHttpUtils.post()
                .url(USERLOGOUT)
                .build()
                .execute(new StringCallback() {
                    @Override
                    public void onError(Call call, Exception e, int id) {
                        Log.e("==logout-error", e.getMessage());
                    }

                    @Override
                    public void onResponse(String response, int id) {
                        if (popupWindow_success.isShowing()) {
                            popupWindow_success.dismiss();
                        }

                        if (popupWindow_failed.isShowing()) {
                            popupWindow_failed.dismiss();
                        }

                        Intent intent = new Intent(KindDetailsActivity.this, DeskTopActivity.class);
                        startActivity(intent);
                        Log.e("==logout-", response);
                        finish();
                    }
                });
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        mSpeechSynthesizer.release();
    }

//    @Override
//    public void onCardReadSuccess(String cardNo) {
//
//    }
//
//    @Override
//    public void getLimitSwitchStatus(int id) {
//
//    }
//
//    @Override
//    public void pushRod(String direction) {
//
//    }
//
//    @Override
//    public void pullRod(String direction) {
//
//    }
//
//    @Override
//    public void getWeight() {
//        Log.e("--->","-->物品重量");
//        if (isKg) {
////            ElecScale.getInstance().setOnDataReceiveListener(new ElecScale.OnGetScaleVale() {
////                @Override
////                public void onGetScaleValue(double value) {
////                    weightOrNums = value;
////                    Message msg = Message.obtain();
////                    msg.what = 1;
////                    handler.sendMessage(msg);
////                }
////            });
//            try {
//                int weight = EnsenLib.getInstance().getWeight();
//                weightTv.setText((weight/1000)+"");
//                weightOrNums = weight;
//                Message msg = Message.obtain();
//                msg.what = 1;
//                handler.sendMessage(msg);
//            }catch (EnsenLibException e){
//                Log.e(TAG, "onClick: get Weight Err: " + e.getMessage());
//            }catch (OverWeightException e){
//                Log.e(TAG, "onClick: get Weight Err: " + e.getMessage());
//            };
//        }
//
//
//    }
//
//    @Override
//    public void setBalance(int selfWeight) {
//
//    }
//
//    @Override
//    public void print(String type, String no) {
//        Log.e("--->","-->打印");
////        Printer.getInstance().sendCmds(type,no);
//        try {
//            EnsenLib.getInstance().print(type, no);
//        }catch (Exception e){
//            Log.e(TAG, "onClick: Printer print Err:" +
//                    e.toString());
//        }
//
//
//    }
//
//    @Override
//    public void rotate(String direction) {
//
//    }
//
//    @Override
//    public void openLock(int id) {
//        Log.e("--->","打开"+id+"号箱门");
//       EnsenLib.getInstance().openLock(id);
//    }
}
